#include "PrecompiledHeader.h"
#include "CtrlButton.h"

/*--------------------------------------------------------------------------*/

CtrlButton::CtrlButton(ICtrl* parent, const std::string& name) :
	ICtrl(parent, name)
{
	this->m_cmd = "echo This button has no commands attached.";
}

/*--------------------------------------------------------------------------*/

CtrlButton::~CtrlButton()
{

}

/*--------------------------------------------------------------------------*/

void CtrlButton::Draw()
{
	if (this->m_CMouseOver)
	{
		//OpenGL::DrawRectangle(this->m_screenPosition, this->m_screenSizePosition, this->m_hoverBackground);
	}
	else
	{
		//OpenGL::DrawRectangle(this->m_screenPosition, this->m_screenSizePosition, this->m_background);
	}

	g_Renderer->DrawText2D(this->GetFont(), this->m_label, this->m_screenPosition, this->m_fontColor);

// 	CHud* gf = CHud::getInstance();
// 	CRenderer* cr = CRenderer::getInstance();
// 	glm::vec2 scrpos = this->GetScreenPosition();
//
// 	glm::vec2 aa = scrpos + glm::vec2(1, 1);
// 	glm::vec2 bb = scrpos + this->GetSize() - glm::vec2(1, 1);
//
// 	cr->DrawRectangle(scrpos, scrpos + this->GetSize(), gf->GetSecondaryBackgroundColor());
//
// 	if (this->status_IsLClick)
// 	{
// 		cr->DrawRectangle(aa, bb, gf->GetPrimaryBackgroundColor());
// 	}
// 	else if (this->status_IsCMouseOver == false)
// 	{
// 		cr->DrawRectangle(aa, bb, gf->GetSecondaryBackgroundColor());
// 	}
// 	else
// 	{
// 		cr->DrawRectangle(aa, bb, gf->GetButtonColor());
// 	}
// }
//
// 	glm::vec2 labelMargin;
// 	labelMargin.x = (this->GetSize().x - this->GetFont()->GetWidth(this->GetLabel())) * 0.5f;
// 	labelMargin.y = (this->GetSize().y - this->GetFont()->GetSize()) * 0.5f;
//
// 	cr->PutText(this->GetLabel(), scrpos + labelMargin, gf->GetTextColor(), this->GetFont());

	this->CallChildsDraw();
}

/*--------------------------------------------------------------------------*/

void CtrlButton::SetCommand(const std::string& cmd)
{
	this->m_cmd = cmd;
}

/*--------------------------------------------------------------------------*/

const std::string& CtrlButton::GetCommand() const
{
	return this->m_cmd;
}

/*--------------------------------------------------------------------------*/

void CtrlButton::ReadSpecials(std::ifstream& file, const std::string& firstWord)
{
	if (firstWord == "cmd")
	{
		std::string str;
		char tmp = '\0';

		while ((tmp = file.get()) != '\n')
		{
			str += tmp;
		}

		this->SetCommand(str);
	}
	else
	{
		this->ReadBasicGfx(file, firstWord);
		this->ReadTextSupport(file, firstWord);
	}
}

/*--------------------------------------------------------------------------*/

void CtrlButton::OnLMBClick(const glm::vec2& clickPosition)
{
	g_Shell->Execute(this->m_cmd);
}

/*--------------------------------------------------------------------------*/